package com.ys.controller.web.business;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.ys.config.MenuId;
import com.ys.entity.business.TsOrganizationAccount;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/business/tsOrganizationAccount")
public class WebTsOrganizationAccountController {
	

	@Autowired
	private CommonApiService<TsOrganizationAccount> commonApiService;
	
	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response,@RequestParam Map<String, Object> params){
		try {
			TsOrganizationAccount entity = new TsOrganizationAccount()
					.setStatus(1)
					.setCreateTime(new Date());
			return commonApiService.toAdd(model,entity, MenuId.TS_OrganizationAccount, null, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}

	public Map<String, Object> setExtraParam(Map<String,Object> entity){
		Map<String, Object> extraParam = new HashMap<String, Object>();
		//关联数据
		if(entity.get("userName")!=null)extraParam.put("userName", entity.get("userName"));
		if(entity.get("deptName")!=null)extraParam.put("deptName", entity.get("deptName"));
		if(entity.get("orgid")!=null)extraParam.put("orgid", entity.get("orgid"));
		if(entity.get("roleName")!=null)extraParam.put("roleName", entity.get("roleName"));
		if(entity.get("vgsbm")!=null)extraParam.put("vgsbm", entity.get("vgsbm"));
		if(entity.get("vgsjc")!=null)extraParam.put("vgsjc", entity.get("vgsjc"));



		return extraParam;
	}

	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, Long id){
		if (id == null){
			return W.error(model, "id不能为空");
		}

		Map<String, Object> entity = new TsOrganizationAccount().setId(id)
				.addLeftJoin("THR_OperateCompanyApply B on B.VGsBm = A.Vgsbm")
				.addLeftJoin("TS_OrganizationInfo C on C.Orgid = A.Orgid")
				.addLeftJoin("TS_UserInfo D on C.HeadUserId = D.UserId ")
				.addField("C.OrgName  AS deptName")
				.addField("B.VGsJc  AS vgsjc")
				.addField("D.RoleName  AS roleName")
				.addField("(D.UserId+ ',' +D.UserName)  AS userName")
				.queryFirstMap();
		if (entity == null){
			return W.error(model, "该信息不存在!");
		}
		if (new TsOrganizationAccount().setId(id).count() == 0){
			return W.error(model, "该信息不存在!");
		}

		try {
			Map<String, Object> extraParam = setExtraParam(entity);
			model.addAttribute("onProcess", 0);
			return commonApiService.toEdit(model, id, new TsOrganizationAccount(), MenuId.TS_OrganizationAccount, extraParam, null);
		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据出错，请联系客服!");
		}
	}

}
